home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / WINPROGS / PCL4W10.ZIP / SIMPLE.C < prev    next >
Text File  |  1994-01-27  |  8KB  |  323 lines

  1. /*
  2. **                    --- simple.c ---
  3. **
  4. **  EXAMPLE CODE: Very simple terminal emulator.
  5. **
  6. **  This example program (not the PCL4C library) is donated to
  7. **  the Public Domain by MarshallSoft Computing, Inc. It is
  8. **  provided as an example of the use of the PCL4W.
  9. **
  10. */
  11.  
  12. #include "windows.h"
  13. #include "simple.h"
  14. #include "pcl4w.h"
  15. #include "sioerror.h"
  16. #include "ascii.h"
  17. #include "simpl_io.h"
  18. #include "expect.h"
  19. #include "config.h"
  20. #include "paint.h"
  21. #include "line.h"
  22. #include "about.h"
  23.  
  24. /* public globals */
  25. HWND hMainWnd;            /* main window handle */
  26. HWND hInfoWnd;            /* popup handle */
  27. HANDLE hInstance;         /* program instance */
  28. int OnLineFlag = FALSE;   /* TRUE: online */
  29. int FatalFlag = FALSE;    /* TRUE: fatal error */
  30. /* miscellaneous functions */
  31. void ErrorCheck(int);
  32.  
  33. /*
  34. ** PostMainHandle() is required only for the
  35. ** Shareware version of PCL4W.
  36. */
  37.  
  38. #if __cplusplus
  39. extern "C" void FAR PASCAL PostMainHandle(HWND);
  40. #else
  41. extern void FAR PASCAL PostMainHandle(HWND);
  42. #endif
  43.  
  44. int PASCAL WinMain(HANDLE hInst,HANDLE hPrevInstance,
  45.                    LPSTR lpCmdLine,int nCmdShow)
  46. {WNDCLASS  wc;
  47.  MSG msg;
  48.  BOOL Result;
  49.  if(!hPrevInstance)
  50.    {/* register main window class */
  51.     wc.style = CS_HREDRAW | CS_VREDRAW;
  52.     wc.lpfnWndProc = MainWndProc;
  53.     wc.cbClsExtra = 0;
  54.     wc.cbWndExtra = 0;
  55.     wc.hInstance = hInst;
  56.     wc.hIcon = LoadIcon(hInst, "SimpleIcon");
  57.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  58.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  59.     wc.lpszMenuName =  "SimpleMenu";
  60.     wc.lpszClassName = "SimpleWClass";
  61.     Result = RegisterClass(&wc);
  62.     if(!Result) return FALSE;
  63.    }
  64.  
  65.  /* create main window */
  66.  hInstance = hInst;
  67.  hMainWnd = CreateWindow(
  68.         "SimpleWClass",   "Simple",       WS_OVERLAPPEDWINDOW,
  69.         CW_USEDEFAULT,    CW_USEDEFAULT,
  70.         CW_USEDEFAULT,    CW_USEDEFAULT,
  71.         NULL,             NULL,
  72.         hInstance,        NULL);
  73.  ShowWindow(hMainWnd, nCmdShow);
  74.  UpdateWindow(hMainWnd);
  75.  
  76.  /* window control loop */
  77.  
  78.  while(GetMessage(&msg,NULL,NULL,NULL))
  79.    {
  80.     TranslateMessage(&msg);
  81.     DispatchMessage(&msg);
  82.    }
  83.  return (msg.wParam);
  84. } /* end WinMain */
  85.  
  86. long FAR PASCAL MainWndProc(HWND hWindow,UINT message,WPARAM wParam,LPARAM lParam)
  87. {
  88.  UINT idTimer;
  89.  HDC hDC;
  90.  PAINTSTRUCT ps;
  91.  int i;
  92.  int TheChar;
  93.  char Temp[82];
  94.  HMENU hMenu;
  95.  int Count;
  96.  static FARPROC lpProcAbout;
  97.  static int ThePort;
  98.  
  99.  hMainWnd = hWindow;
  100.  switch (message)
  101.     {
  102.      case WM_COMMAND:
  103.          switch(wParam)
  104.            {case MSG_ABOUT:
  105.               DialogBox(hInstance,"AboutBox",hMainWnd,lpProcAbout);
  106.               break;
  107.  
  108.             case MSG_LOAD:
  109.               if(!ExpectOffLine()) break;
  110.               LoadConfig();
  111.               break;
  112.  
  113.             case MSG_SAVE:
  114.               if(!ExpectOffLine()) break;
  115.               SaveConfig();
  116.               SetTitle();
  117.               break;
  118.  
  119.             case MSG_ONLINE:
  120.               if(!ExpectOffLine()) break;
  121.               if(FatalFlag) ErrorMessage("Fatal Error");
  122.               else
  123.                 {/* try to go on-line */
  124.                  GoOnLine();
  125.                  if(!OnLineFlag) break;
  126.                  ThePort = GetPort();
  127.                 }
  128.               break;
  129.  
  130.             case MSG_OFFLINE:
  131.               GoOffLine();
  132.               break;
  133.  
  134.             case MSG_EXIT:
  135.               GoOffLine();
  136.               KillTimer(hMainWnd,idTimer);
  137.               PostQuitMessage(0);
  138.               break;
  139.  
  140.             case MSG_1200:
  141.               SetBaud(Baud1200);
  142.               break;
  143.  
  144.             case MSG_2400:
  145.               SetBaud(Baud2400);
  146.               break;
  147.  
  148.             case MSG_4800:
  149.               SetBaud(Baud4800);
  150.               break;
  151.  
  152.             case MSG_9600:
  153.               SetBaud(Baud9600);
  154.               break;
  155.  
  156.             case MSG_19200:
  157.               SetBaud(Baud19200);
  158.               break;
  159.  
  160.             case MSG_38400:
  161.               SetBaud(Baud38400);
  162.               break;
  163.  
  164.             case MSG_57600:
  165.               SetBaud(Baud57600);
  166.               break;
  167.  
  168.             case MSG_115200:
  169.               SetBaud(Baud115200);
  170.               break;
  171.  
  172.             case MSG_COM1:
  173.               SetPort(COM1);
  174.               break;
  175.  
  176.             case MSG_COM2:
  177.               SetPort(COM2);
  178.               break;
  179.  
  180.             case MSG_COM3:
  181.               SetPort(COM3);
  182.               break;
  183.  
  184.             case MSG_COM4:
  185.               SetPort(COM4);
  186.               break;
  187.  
  188.             case MSG_NONE:
  189.               SetParity(NoParity);
  190.               break;
  191.  
  192.             case MSG_EVEN:
  193.               SetParity(EvenParity);
  194.               break;
  195.  
  196.             case MSG_ODD:
  197.               SetParity(OddParity);
  198.               break;
  199.  
  200.             case MSG_1_SB:
  201.               SetStopBits(OneStopBit);
  202.               break;
  203.  
  204.             case MSG_2_SB:
  205.               SetStopBits(TwoStopBits);
  206.               break;
  207.  
  208.             case MSG_7_DB:
  209.               SetWordLength(WordLength7);
  210.               break;
  211.  
  212.             case MSG_8_DB:
  213.               SetWordLength(WordLength8);
  214.               break;
  215.  
  216.             default:
  217.               return (DefWindowProc(hMainWnd, message, wParam, lParam));
  218.            }
  219.          break;
  220.  
  221.     case WM_CREATE:
  222.  
  223. /*
  224. ** You must call PostMainHandle() before attemping to go online.
  225. ** This is required only for the Shareware version of PCL4W.
  226. */
  227.       PostMainHandle(hMainWnd);
  228.  
  229.       /* check "OFFLINE" menu item */
  230.       hMenu = GetMenu(hMainWnd);
  231.       CheckMenuItem(hMenu,MSG_OFFLINE,MF_BYCOMMAND | MF_CHECKED);
  232.       /* create AboutDlgProc() thunk */
  233.       lpProcAbout = MakeProcInstance(AboutDlgProc, hInstance);
  234.       /* init configuration */
  235.       InitConfig();
  236.       LoadConfig();
  237.       /* initialize paint module */
  238.       InitPaint();
  239.       /* start timer */
  240.       idTimer = SetTimer(hMainWnd,1,125,NULL);
  241.       if(idTimer==0)
  242.          {ErrorMessage("No timers remaining !");
  243.           FatalFlag = TRUE;
  244.          }
  245.  
  246. #if 1
  247.  /*** Custom Configuration: DigiBoard PC/8 for COM3 - COM10 ***/
  248.  /* use 0x140 for odd IRQs & 0x141 for even IRQs */
  249.  SioPorts(10,COM3,0x140);
  250.  /* set DigiBoard UART addresses for COM3 to COM10 */
  251.  for(i=0;i<8;i++)
  252.    {/* set DigiBoard port address */
  253.     ErrorCheck(SioUART(COM3+i,0x100+8*i) );
  254.     /* set DigiBoard for IRQ5 */
  255.     ErrorCheck(SioIRQ(COM3+i,IRQ5) );
  256.    }
  257. #endif
  258.  
  259.       break;
  260.  
  261.     case WM_CHAR:
  262.       PutChar(ThePort, (char)wParam );
  263.       break;
  264.  
  265.     case WM_TIMER:
  266.       /* fatal error ? */
  267.       if(FatalFlag) break;
  268.       if(!OnLineFlag) break;
  269.       /* fetch line of up to 82 chars */
  270.       Count = 0;
  271.       for(i=0;i<82;i++)
  272.         {TheChar = GetChar(ThePort);
  273.          /* character available ? */
  274.          if(TheChar==-1) break;
  275.          Temp[Count++] = TheChar;
  276.          if((char)TheChar==(char)LF) break;
  277.         } /* end while */
  278.       if(Count>0) WriteTheString(Temp,Count);
  279.       break;
  280.  
  281.     case WM_SETFOCUS:
  282.       /* create client area caret */
  283.       CreateCaret(hMainWnd,NULL,3,10);
  284.       SetCaretPos(GetXposition(),GetYposition());
  285.       ShowCaret(hMainWnd);
  286.       ShowCaret(hMainWnd);
  287.       break;
  288.  
  289.     case WM_KILLFOCUS:
  290.       DestroyCaret();
  291.       break;
  292.  
  293.     case WM_PAINT:
  294.       HideCaret(hMainWnd);
  295.       hDC = BeginPaint(hMainWnd, &ps);
  296.       SetMapMode(hDC,MM_ANISOTROPIC);
  297.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  298.       PaintMain(hDC,&ps);
  299.       EndPaint(hMainWnd,&ps);
  300.       SetCaretPos(GetXposition(),GetYposition());
  301.       ShowCaret(hMainWnd);
  302.       break;
  303.  
  304.     case WM_DESTROY:
  305.       GoOffLine();
  306.       if(idTimer) KillTimer(hMainWnd,idTimer);
  307.       PostQuitMessage(0);
  308.       break;
  309.  
  310.     default:
  311.       return (DefWindowProc(hMainWnd, message, wParam, lParam));
  312.    }
  313.  return (NULL);
  314. } /* end MainWndProc */
  315.  
  316. void ErrorCheck(int Code)
  317. {/* trap PCL error codes */
  318.  if(Code<0)
  319.      {SioDone(GetPort());
  320.       FatalFlag = TRUE;
  321.      }
  322. } /* end ErrorCheck */
  323.